package com.axter.libs.photo.choosephoto;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;

import com.axter.libs.photo.display.IChoosePhotoCallBackInterface;
import com.axter.libs.photo.display.IPhotoDisplayInterface;

/**
 * Created by zhaobo on 2016/10/13.
 */

public class ChoosePhotoIntent extends Intent {
    /** 总共可以选择多少资源 最少1 */
    private int maxSize = 1;
    /** 最大照片选择数 */
    private int maxImageSize = 1;
    /** 最大视频选择数 */
    private int maxVideoSize = 1;
    /** 单选模式 */
    private boolean isSingle = false;
    /** 显示gif */
    private boolean isShowGif = false;
    /** 显示视频 */
    private boolean isShowVideo = false;
    /** 显示拍照 */
    private boolean isShowCamera = false;
    /** 拍照目录 */
    private String cameraCacheDir;
    /** 统一自定义接口 */
    private String choosePhotoCallBackInterfacePath;
    private String photoDisplayInterfacePath;

    public ChoosePhotoIntent setMaxSize(int maxSize) {
        this.maxSize = maxSize;
        return this;
    }

    public ChoosePhotoIntent setMaxVideoSize(int maxVideoSize) {
        this.maxVideoSize = maxVideoSize;
        return this;
    }

    public ChoosePhotoIntent setMaxImageSize(int maxImageSize) {
        this.maxImageSize = maxImageSize;
        return this;
    }

    public ChoosePhotoIntent setSingle(boolean single) {
        isSingle = single;
        return this;
    }

    public ChoosePhotoIntent setShowGif(boolean showGif) {
        isShowGif = showGif;
        return this;
    }

    public ChoosePhotoIntent setShowVideo(boolean showVideo) {
        isShowVideo = showVideo;
        return this;
    }

    public ChoosePhotoIntent setShowCamera(boolean showCamera) {
        isShowCamera = showCamera;
        return this;
    }

    public ChoosePhotoIntent setCameraCacheDir(String cameraCacheDir) {
        this.cameraCacheDir = cameraCacheDir;
        return this;
    }

    public ChoosePhotoIntent setChoosePhotoCallBackInterface(Class<? extends IChoosePhotoCallBackInterface> clazz) {
        this.choosePhotoCallBackInterfacePath = clazz.getName();
        return this;
    }

    public ChoosePhotoIntent setPhotoDisplayInterface(Class<? extends IPhotoDisplayInterface> clazz) {
        this.photoDisplayInterfacePath = clazz.getName();
        return this;
    }

    public ChoosePhotoIntent(Context packageContext) {
        super(packageContext, ChoosePhotoActivity.class);
    }

    private void handleData() {
        // 1.单选图片只能为1张
        if (isSingle) {
            maxImageSize = 1;
        }
        // 不显示视频就不给视频选择配额
        if (!isShowVideo) {
            maxVideoSize = 0;
        }
        // 如果允许的最大数超过总和,就限定主数目
        if (maxSize > maxImageSize + maxVideoSize) {
            maxSize = maxImageSize + maxVideoSize;
        }
        putExtra(ChoosePhotoActivity.EXTRA_MAX_SIZE, maxSize);
        putExtra(ChoosePhotoActivity.EXTRA_MAX_SIZE_IMAGE, maxImageSize);
        putExtra(ChoosePhotoActivity.EXTRA_MAX_SIZE_VIDEO, maxVideoSize);
        putExtra(ChoosePhotoActivity.EXTRA_SINGLE, isSingle);
        putExtra(ChoosePhotoActivity.EXTRA_SHOW_GIF, isShowGif);
        putExtra(ChoosePhotoActivity.EXTRA_SHOW_VIDEO, isShowVideo);
        putExtra(ChoosePhotoActivity.EXTRA_SHOW_CAMERA, isShowCamera);
        putExtra(ChoosePhotoActivity.EXTRA_CAMERA_DIR, cameraCacheDir);
        putExtra(ChoosePhotoActivity.EXTRA_CALLBACK_INTERFACE, choosePhotoCallBackInterfacePath);
        putExtra(ChoosePhotoActivity.EXTRA_DISPLAY_INTERFACE, photoDisplayInterfacePath);
    }

    public void startActivity(Activity activity, int requestCode) {
        handleData();
        activity.startActivityForResult(this, requestCode);
    }

    public void startActivity(Fragment fragment, int requestCode) {
        handleData();
        fragment.startActivityForResult(this, requestCode);
    }

    public void startActivity(android.app.Fragment fragment, int requestCode) {
        handleData();
        fragment.startActivityForResult(this, requestCode);
    }
}
